home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
mint
/
lib
/
mntlib44.zoo
/
mntlib
/
opendir.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-03-01
|
2KB
|
99 lines
/* opendir routine */
/* under MiNT (v0.9 or better) these use the appropriate system calls.
* under TOS or older versions of MiNT, they use Fsfirst/Fsnext
*
* Written by Eric R. Smith and placed in the public domain
*/
#include <stdlib.h>
#include <string.h>
#include <types.h>
#include <limits.h>
#include <dirent.h>
#include <errno.h>
#include <osbind.h>
#include <mintbind.h>
#include "lib.h"
extern int __mint;
extern ino_t __inode; /* in stat.c */
DIR *
opendir(uname)
const char *uname;
{
DIR *d;
long r;
_DTA *olddta;
char name[PATH_MAX];
char dirpath[PATH_MAX];
char *p;
d = malloc(sizeof(DIR));
if (!d) {
errno = ENOMEM;
return d;
}
_unx2dos(uname, name);
if (__mint > 8) {
r = Dopendir(name, 0);
if ( (r & 0xff000000L) == 0xff000000L ) {
errno = (int) -r;
free(d);
return 0;
}
d->handle = r;
d->buf.d_off = 0;
return d;
}
/* TOS emulation routines */
p = name;
if (p) {
/* find the end of the string */
for (p = name; *p; p++) ;
/* make sure the string ends in '\' */
if (*(p-1) != '\\') {
*p++ = '\\';
}
}
strcpy(p, "*.*");
olddta = Fgetdta();
Fsetdta(&(d->dta));
r = Fsfirst(name, 0x17);
Fsetdta(olddta);
if (r == 0) {
d->status = _STARTSEARCH;
} else if (r == -ENOENT) {
d->status = _NMFILE;
} else {
free(d);
errno = (int) -r;
return 0;
}
d->buf.d_off = 0;
/* for rewinddir: if necessary, build a relative path */
if (name[1] == ':') { /* absolute path, no problem */
dirpath[0] = 0;
} else {
dirpath[0] = Dgetdrv() + 'A';
dirpath[1] = ':';
dirpath[2] = 0;
if (*name != '\\')
(void)Dgetpath(dirpath+2, 0);
}
d->dirname = malloc(strlen(dirpath)+strlen(name)+1);
if (d->dirname) {
strcpy(d->dirname, dirpath);
strcat(d->dirname, name);
}
return d;
}